package com.zl.controller;

import com.zl.model.Userinfo;
import com.zl.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/userinfo")
public class UserinfoController {

    @Autowired
    @Qualifier("UserinfoServiceImpl")
    private UserinfoService userinfoService;

    /**
     * 查询全部
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll2(Model model){
        List<Userinfo> list1=userinfoService.findAll();
        model.addAttribute("list1",list1);
        return "main2";
    }
    /**
     * 登录
     * @param
     * @return
     */
        //1.去登录

        @RequestMapping("tologin")
        public String tologin(){
            return "login";
        }
        //2.登录

        @RequestMapping("login")
        public String login(Userinfo userinfo){
            Userinfo u=userinfoService.login(userinfo);
            if (u==null){
                return "login";
            }
            return "main";
        }

    /**
     * 查一个
     * @param model
     * @param user_id
     * @return
     */
     @RequestMapping("/findOne")
    public String findone(Model model,int user_id){
            Userinfo u=userinfoService.findOne(user_id);
            List<Userinfo> list=userinfoService.findAll();
            model.addAttribute("u",u);
            model.addAttribute("list",list);
            return "uupdate";
     }

    /**
     * 添加
     * @return
     */
        //1.去添加

        @RequestMapping("/toinsert")
        public String toinsert(Model model){
            List<Userinfo> list=userinfoService.findAll();
            model.addAttribute("list",list);
            return "uadd";
        }
        //2.添加

        @RequestMapping("/insert")
        public String insert(Userinfo userinfo){
            userinfoService.insert(userinfo);
            return "redirect:/userinfo/findAll";
        }

    /**
     * 删除
     * @param user_id
     * @return
     */
    @RequestMapping("/remove/{uid}")
    public String remove(@PathVariable("uid") int user_id){
            userinfoService.remove(user_id);
            return "redirect:/userinfo/findAll";
     }

    /**
     * 修改
     * @param userinfo
     * @return
     */
     @RequestMapping("/update")
    public String update(Userinfo userinfo){
        userinfoService.update(userinfo);
        return "redirect:/userinfo/findAll";
     }
}
